﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Channels;
using ChatInterfaces;
using CHATCLIENT;


namespace CHATCLIENTVIEWER
{
    public partial class Form1 : Form, IView
    {
        private IChatClient chatClient { get; set; }

        public Form1()
        {
            chatClient = new ClientChat();
            chatClient.RegistView(this);
            InitializeComponent();
            this.FormClosing += chatClient.ClientChat_FormClosing;
            labelId.Text = Settings.Name + " - " + Settings.BaseAddress;
            EnableControls(false);
            EnableLogout(false);
        }

        private void SendMessage_Click(object sender, EventArgs e)
        {
            if (comboBoxUsers.SelectedItem != null)
                chatClient.SendPrivateMessage(((ClientToPass)comboBoxUsers.SelectedItem), textBoxNewMessage.Text);
        }

        private void buttonLogIn_Click(object sender, EventArgs e)
        {
            chatClient.DoLogin(textBoxUserName.Text, textBoxPWD.Text);
        }

        public void ApendNewMessage(string msg)
        {
            textBoxMessages.AppendText(string.Format("{0}{1}", Environment.NewLine, msg));
        }

        public void RefreshClientList(List<ClientToPass> x)
        {
            //if (x.Count > 0)
            //{
                listBoxUsers.DataSource = null;
                listBoxUsers.DataSource = x;
                listBoxUsers.DisplayMember = "Username";

                comboBoxUsers.DataSource = null;
                comboBoxUsers.DataSource = x;
                comboBoxUsers.DisplayMember = "Username";
            //}

                if (comboBoxUsers.Items != null && comboBoxUsers.Items.Count > 0)
                    comboBoxUsers.SelectedIndex = 0;
        }

        private void buttonLogout_Click(object sender, EventArgs e)
        {
            chatClient.DoLogout();
        }

        public void EnableLogin(bool IsEnabled)
        {
            buttonLogIn.Enabled = IsEnabled;
        }

        public void EnableLogout(bool IsEnabled)
        {
            buttonLogout.Enabled = IsEnabled;
        }

        public void EnableControls(bool IsEnabled)
        {
            textBoxNewMessage.Enabled = IsEnabled;
            SendMessage.Enabled = IsEnabled;
            comboBoxUsers.Enabled = IsEnabled;
        }
    }
}
